package com.ning.activitys.skysound.fragment.mymusic;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.ning.activitys.skysound.R;
import com.ning.activitys.skysound.activity.MainActivity;
import com.ning.activitys.skysound.activity.PlayActivity;
import com.ning.activitys.skysound.adpter.AllMusicAdapter;
import com.ning.activitys.skysound.base.BaseFragment;
import com.ning.activitys.skysound.base.MyApplication;
import com.ning.activitys.skysound.beans.MusicDetailsBeans;
import com.ning.activitys.skysound.datas.MusicDetailsTool;
import com.ning.activitys.skysound.service.PlayService;

import java.util.ArrayList;


/**
 * Created by dllo on 16/1/8.
 */
public class AllMusicFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener {

    private AllMusicAdapter adapter;
    private ListView listView;
    private MainActivity mainActivity;
    private ArrayList<MusicDetailsBeans> musicDetailsData = new ArrayList<>();



    private volatile static AllMusicFragment allMusicFragment;//单例的引用


    public static AllMusicFragment newInstance(){
        AllMusicFragment allMusicFragment = new AllMusicFragment();
        return allMusicFragment;
    }

    //单例模式创建对象
    public static AllMusicFragment getInstance() {
        if (allMusicFragment == null) {
            //线程锁,只有一个线程可以调用
            synchronized (AllMusicFragment.class) {
                if (allMusicFragment == null) {
                    allMusicFragment = new AllMusicFragment();
                }
            }
        }
        return allMusicFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_all_music,null);
        return view;

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mainActivity = (MainActivity) context;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        listView = (ListView) view.findViewById(R.id.list_view_my_music);

        //直接从服务里拿数据
        //musicDetailsData = (ArrayList)mainActivity.playService.getMusicDetailsData();

        musicDetailsData = MusicDetailsTool.getMusicDetails(mainActivity);
        adapter = new AllMusicAdapter(mainActivity,musicDetailsData);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        mainActivity.bindPlayService();
    }

    @Override
    public void onPause() {
        super.onPause();
        mainActivity.unBindPlayService();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }


    //listView Item 的监听方法
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        PlayService.IS_NET_MUSIC = 1;

        if (mainActivity.playService.getPlatList() != PlayService.ALL_MUSIC){

            mainActivity.playService.setPlatList(PlayService.ALL_MUSIC);
            mainActivity.playService.setMusicDetailsData(MusicDetailsTool.getMusicDetails(mainActivity));
        }
        mainActivity.playService.play(position);

        /**
         * 点击添加最近播放
         * */
        MusicDetailsBeans musicDetailsBeans = mainActivity.playService.getMusicDetailsData().get(position);

        MyApplication myApplication = (MyApplication) mainActivity.getApplication();
        try {

           MusicDetailsBeans beans =  myApplication.dbUtils.findFirst(Selector.from(MusicDetailsBeans.class)
                    .where("myID", "=", musicDetailsBeans.getId()));

           if (beans == null){
               musicDetailsBeans.setMyID(musicDetailsBeans.getId());
               musicDetailsBeans.setPlayRecentTime(System.currentTimeMillis());
               myApplication.dbUtils.save(musicDetailsBeans);
           }else {
               beans.setPlayRecentTime(System.currentTimeMillis());
               myApplication.dbUtils.update(beans,"playRecentTime");
           }

        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    //本类点击事件的监听方法
    @Override
    public void onClick(View v) {
         switch (v.getId()){

         }
    }

//    public void initData(){
//        //获取音乐数据
//        musicDetailsData = mainActivity.playService.getMusicDetailsData();
//        adapter = new AllMusicAdapter(mainActivity,musicDetailsData);
//        listView.setAdapter(adapter);
//    }

}
